<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单模板编译器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f8ff;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }

        h1 {
            color: #4CAF50;
            text-align: center;
        }

        label {
            font-size: 16px;
            color: #333;
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f1f1f1;
            border-radius: 5px;
            border: 1px solid #ddd;
        }

        .result p {
            margin: 5px 0;
            font-size: 14px;
        }

        .result:empty {
            display: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>简单模板编译器</h1>
        <label for="templateInput">请输入模板（例如：你好，{{name}}！你有{{count}}条新消息）：</label>
        <input type="text" id="templateInput" placeholder="例如：你好，{{name}}！你有{{count}}条新消息">

        <label for="dataInput">请输入数据（例如：{"name": "张三", "count": 5}）：</label>
        <input type="text" id="dataInput" placeholder='例如：{"name": "张三", "count": 5}'>

        <button onclick="renderTemplate()">渲染模板</button>

        <div id="result" class="result"></div>
    </div>

    <script>
        // 简单模板编译器函数
        const simpleTemplateCompiler = template => data =>
            template.replace(/\{\{(.*?)\}\}/g, (match, key) => data[key.trim()] || match);

        function renderTemplate() {
            const template = document.getElementById('templateInput').value;
            const dataInput = document.getElementById('dataInput').value;

            // 尝试将输入的数据解析为 JSON 对象
            let data;
            try {
                data = JSON.parse(dataInput);
            } catch (e) {
                document.getElementById('result').innerHTML = '<p style="color: red;">数据格式无效，请输入正确的 JSON。</p>';
                return;
            }

            // 使用模板编译器渲染模板
            const compiledTemplate = simpleTemplateCompiler(template);
            const result = compiledTemplate(data);

            // 显示渲染后的结果
            document.getElementById('result').innerHTML = `<p>${result}</p>`;
        }
    </script>
</body>

</html>